package com.credit.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.credit.common.ServerResponse;
import com.credit.mapper.administrativeMapper;
import com.credit.mapper.balanceSheetMapper;
import com.credit.mapper.creditLoanMapper;
import com.credit.mapper.debtRiskMapper;
import com.credit.mapper.economicGainMapper;
import com.credit.mapper.enterpriseInformationMapper;
import com.credit.mapper.environmentalMapper;
import com.credit.mapper.flowsMapper;
import com.credit.mapper.frameworkMapper;
import com.credit.mapper.industryMapper;
import com.credit.mapper.jobMapper;
import com.credit.mapper.judicialMapper;
import com.credit.mapper.managementSystemMapper;
import com.credit.mapper.occupationalHealthMapper;
import com.credit.mapper.otherProductsMapper;
import com.credit.mapper.patentTypeMapper;
import com.credit.mapper.professionalWorkMapper;
import com.credit.mapper.profitMapper;
import com.credit.mapper.profitabilityMapper;
import com.credit.mapper.qualificationMapper;
import com.credit.mapper.qualityAssetsMapper;
import com.credit.mapper.qualityControlMapper;
import com.credit.mapper.seniorExecutiveMapper;
import com.credit.mapper.socialContributionMapper;
import com.credit.mapper.violationOperationMapper;
import com.credit.pojo.administrative;
import com.credit.pojo.balanceSheet;
import com.credit.pojo.companyQualification;
import com.credit.pojo.copyWriter;
import com.credit.pojo.creditLoan;
import com.credit.pojo.debtRisk;
import com.credit.pojo.economicGain;
import com.credit.pojo.enterpriseInformation;
import com.credit.pojo.environmental;
import com.credit.pojo.flows;
import com.credit.pojo.framework;
import com.credit.pojo.industry;
import com.credit.pojo.job;
import com.credit.pojo.judicial;
import com.credit.pojo.managementSystem;
import com.credit.pojo.occupationalHealth;
import com.credit.pojo.otherProducts;
import com.credit.pojo.patentType;
import com.credit.pojo.professionalWork;
import com.credit.pojo.profit;
import com.credit.pojo.profitability;
import com.credit.pojo.qualification;
import com.credit.pojo.qualityAssets;
import com.credit.pojo.qualityControl;
import com.credit.pojo.seniorExecutive;
import com.credit.pojo.socialContribution;
import com.credit.pojo.violationOperation;
import com.credit.service.ISettingsService;
import com.credit.utlis.ImgUpload;
import com.credit.vo.GrandeVo;
import com.credit.vo.RateVo;

/**
 * 系统设置
 * @author Administrator
 *
 */
@CrossOrigin(origins = "*", maxAge = 3600)//跨越请求配置
@RestController
@Controller
public class SettingsController {

	
	@Autowired
	private ISettingsService settingsService;
	
	@Autowired
	private enterpriseInformationMapper enterpriseInformationMapper;
	
	@Autowired
	private balanceSheetMapper balanceSheetMapper;
	
	@Autowired
	private flowsMapper flowsMapper;
	
	@Autowired
	private profitMapper profitMapper;

	@Autowired
	private frameworkMapper frameworkMapper;
	
	@Autowired
	private managementSystemMapper managementSystemMapper;
	
	@Autowired
	private seniorExecutiveMapper seniorExecutiveMapper;
	
	@Autowired
	private jobMapper jobMapper;

	
	@Autowired
	private patentTypeMapper patentTypeMapper;

	
	@Autowired
	private administrativeMapper administrativeMapper;
	
	@Autowired
	private judicialMapper judicialMapper;
	
	@Autowired
	private violationOperationMapper violationOperationMapper;
	
	@Autowired
	private creditLoanMapper creditLoanMapper;
	
	@Autowired
	private qualityControlMapper qualityControlMapper;
	
	@Autowired
	private environmentalMapper environmentalMapper;
	
	@Autowired
	private occupationalHealthMapper occupationalHealthMapper;
	
	@Autowired
	private otherProductsMapper otherProductsMapper;
	
	@Autowired
	private qualificationMapper qualificationMapper;
	
	@Autowired
	private professionalWorkMapper professionalWorkMapper;
	
	@Autowired
	private economicGainMapper economicGainMapper;
	
	@Autowired
	private debtRiskMapper debtRiskMapper;
	
	@Autowired
	private qualityAssetsMapper qualityAssetsMapper;
	
	@Autowired
	private profitabilityMapper profitabilityMapper;
	
	@Autowired
	private socialContributionMapper socialContributionMapper; 
	
	@Autowired
	private industryMapper industryMapper;
	
	
	/**
	 * 显示公司资质
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "selectCompanyQualification.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectCompanyQualification(int id) {
		
		return settingsService.selectCompanyQualification(id);
	}
	
	
	/**
	 * 增加公司资质
	 * @param c
	 * @param photo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addCompanyQualification.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse addCompanyQualification(companyQualification c,@RequestParam(value = "file", required = false) MultipartFile photo,HttpServletRequest request) {
		try {
			String url = ImgUpload.PictureUpload(photo, request);
			c.setUrl(url);
		} catch (IOException e) {
		
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return settingsService.insertCompanyQualification(c);
	}
	
	
	
	/**
	 * 修改公司资质
	 * @param c
	 * @param photo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "upCompanyQualification.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse upCompanyQualification(companyQualification c,@RequestParam(value = "file", required = false) MultipartFile photo,HttpServletRequest request) {
		try {
			String url = ImgUpload.PictureUpload(photo, request);
			c.setUrl(url);
		} catch (IOException e) {
			
			e.printStackTrace();
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return settingsService.updateCompanyQualification(c);
	}
	
	
	
	
	
	/**
	 * 删除公司资质
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delCompanyQualification.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse delCompanyQualification(int id) {
		
		return settingsService.deleteCompanyQualification(id);
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 查看公司文案
	 * @param c
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "selectCopyWriter.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectCopyWriter(copyWriter c,@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		
		
		return settingsService.selectCopyWriter(c, pageNum, pageSize);
	}
	
	
	

	/**
	 * 查看单个文案详情
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "selectOneCopyWriter.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse selectOneCopyWriter(Integer id) {
		
		
		return settingsService.selectOneCopyWriter(id);
	}
	
	
	
	
	
	/**
	 * 增加文案
	 * @param c
	 * @return
	 */
	@RequestMapping(value = "addCopyWriter.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse addCopyWriter(copyWriter c) {
		
		
		return settingsService.insertCopyWriter(c);
	}
	
	
	
	
	/**
	 * 修改文案
	 * @param c
	 * @return
	 */
	@RequestMapping(value = "upCopyWriter.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse upCopyWriter(copyWriter c) {
		
		
		return settingsService.updateCopyWriter(c);
	}
	
	
	
	
	/**
	 * 删除文案
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delCopyWriter.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse delCopyWriter(Integer id) {
		
		
		return settingsService.deleteCopyWriter(id);
	}
	
	
	/**
	 * 评级打分
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "grade.do", method = RequestMethod.POST)
	@ResponseBody
	public ServerResponse grade(Integer id) {
		double sum = 0;    //总分数
		double count = 0;   //小结分数
		
		GrandeVo grande = new GrandeVo();
		
		//获取当前年份
		Calendar date = Calendar.getInstance();
        int year = date.get(Calendar.YEAR);
         
		enterpriseInformation enterpriseInformation = enterpriseInformationMapper.selectEnterpriseInformation(id);
		int ysars = Integer.parseInt(enterpriseInformation.getEstablishmenttime().substring(0, 3));          //获取公司创建年份
		
		//历史沿革
		count = ((year-ysars)/5)*2;
		if(count >= 2) {
			grande.setHistoricalDevelopment(2);
			sum=sum+2;
		}else {
			grande.setHistoricalDevelopment(count);
			sum=sum+count;
		}
		
		
		//人力资源
		count = 0;
		int number = 0;
		
		double state = 0;
		double province = 0;
		double city = 0;
		List<seniorExecutive> seniorExecutiveList = seniorExecutiveMapper.selectSeniorExecutive(id);
		seniorExecutive seniorExecutive = new seniorExecutive();
		for(int i = 0;i<seniorExecutiveList.size();i++) {
			seniorExecutive = seniorExecutiveList.get(i);
			
			count=count+seniorExecutive.getState()+seniorExecutive.getProvince()*0.2+seniorExecutive.getCity()*0.1;
			if(seniorExecutive.getPosition().equals("董事长")) {
				number++;
				if(seniorExecutive.getEducationbackground().equals("本科") || seniorExecutive.getEducationbackground().equals("研究生") || seniorExecutive.getEducationbackground().equals("硕士") || seniorExecutive.getEducationbackground().equals("博士") || seniorExecutive.getEducationbackground().equals("博士后")) {
					number++;
				}
			}
			if(seniorExecutive.getPosition().equals("总经理")) {
				number++;
				if(seniorExecutive.getEducationbackground().equals("本科") || seniorExecutive.getEducationbackground().equals("研究生") || seniorExecutive.getEducationbackground().equals("硕士") || seniorExecutive.getEducationbackground().equals("博士") || seniorExecutive.getEducationbackground().equals("博士后")) {
					number++;
				}
			}
			
			state = state+seniorExecutive.getState();
			province = province+seniorExecutive.getProvince();
			city = city+seniorExecutive.getCity();
			
		}
		if(count >= 2) {
			grande.setBearThePalm(2);
			sum = sum+2;
		}else {
			grande.setBearThePalm(count);
			sum = sum+count;
		}
		grande.setEducationBackground(number);
		industry industry = industryMapper.selectByIndustry(enterpriseInformation.getIndustryid());
		//从业人员
		job job = jobMapper.selectJob(id);
		
		if(enterpriseInformation.getScaleid() == 3) {
			count = (double)job.getHeadcount()/industry.getSquantity()*4;
		}else if(enterpriseInformation.getScaleid() == 2) {
			count =(double)job.getHeadcount()/industry.getMquantity()*4;
		}else {
			count =(double) job.getHeadcount()/industry.getBquantity()*4;

		}
		

		
		if(count >= 2) {
			grande.setEmployee(2);
		}else {
			grande.setEmployee(count);
		}
		
		
		//组织机构
		framework framework = frameworkMapper.selectFramework(id);
		if(framework.getCondition().equals("完善")) {
			grande.setOrganizationalStructure(4);
		}else {
			if(framework.getUrl() != null) {
				grande.setOrganizationalStructure(3);
			}else {
				grande.setOrganizationalStructure(4);
			}
		}
		
		
		//管理制度
		boolean a = false;
		managementSystem managementSystem =managementSystemMapper.selectManagementSystem(id);
		if(managementSystem.getFinance() == 1) {
            a=true;
		}else if(managementSystem.getManpowerresource() == 1) {
			a=true;
		}else if(managementSystem.getQuality() == 1) {
			a=true;;
		}else if(managementSystem.getSafety() == 1) {
			a=true;
		}else if(managementSystem.getCredit() == 1) {
			a=true;
		}
		
		if(a) {
			grande.setManagementSystem(0);
		}else {
			grande.setManagementSystem(5);
		}
			

		
		
		
		//产品认证
		count = 0;
		List<otherProducts> otherProductsList = otherProductsMapper.selectOtherProducts(id);
		
		if(otherProductsList.isEmpty()) {
			grande.setProductCertification(0);
		}else {
			otherProducts otherProducts = new otherProducts();
			for(int i = 0;i<otherProductsList.size();i++) {
				otherProducts = otherProductsList.get(i);
				if(otherProducts.getAccordingto().equals("未列入强制性认证产品目录的产品，通过自愿性产品认证")) {
					count = 2;
					break;
				}else if(otherProducts.getAccordingto().equals("列入强制性认证产品目录的产品，通过3C认证的产品")) {
					if(count < 1.5) {
						count = 1.5;
					}
				}else if(otherProducts.getAccordingto().equals("未列入强制性认证产品目录的产品，未通过自愿性产品认证")) {
					if(count < 1) {
						count = 1;
					}
				}else {
					if(count < 0.5) {
						count = 0.5;
					}
				}
			}
			grande.setProductCertification(count);
		}
		
		
		//资质情况
		count = 0;
		List<qualification> qualificationList = qualificationMapper.selectQualification(id);
		qualification qualification = new qualification();
		for(int i = 0;i<qualificationList.size();i++) {
			qualification=qualificationList.get(i);
			if(qualification.getLevel().equals("高级")) {
				count=count+2;
			}else {
				count++;
			}
		} 
		if(count >=2) {
			grande.setAptitude(2);
		}else {
			grande.setAptitude(count);
		}
		
       //质量管理
       List<qualityControl> qualityControlList = qualityControlMapper.selectQualityControl(id);
       if(qualityControlList.isEmpty()) {
    	   grande.setQuality(0.5);
       }else {
    	   grande.setQuality(1);
       }
       
       //安全管理
       List<occupationalHealth> occupationalHealthList = occupationalHealthMapper.selectOccupationalHealth(id);
       if(occupationalHealthList.isEmpty()) {
    	   grande.setSafetyManagement(0.5);
       }else {
    	   grande.setSafetyManagement(1);
       }
       
       //环保管理
       List<environmental> environmentalList = environmentalMapper.selectEnvironmental(id);
       if(environmentalList.isEmpty()) {
    	   grande.setEnvironmentalProtection(0.5);
       }else {
    	   grande.setEnvironmentalProtection(1);
       }
       
       //获奖情况
       count = (state*0.4)+(province*0.08)+(city*0.04);
       if(count >= 1) {
    	   grande.setCarry(1);
       }else {
    	   grande.setCarry(count);
       }
       
       
       //业务情况
       state = 0;
       province = 0;
       List<professionalWork> professionalWorkList = professionalWorkMapper.selectProfessionalWork(id);
       professionalWork professionalWork = new professionalWork();
       for(int i = 0;i<professionalWorkList.size();i++) {
    	   professionalWork=professionalWorkList.get(i);
    	   state = state+professionalWork.getFbusiness();
       }
       count = (state*0.5)+(province*0.2);
       if(count >= 3) {
    	   grande.setProfessional(3);
       }else {
    	   grande.setProfessional(count);
       }
       
       //债权债务
       //获取行业标准
       RateVo r=new RateVo();
       r.setIndid(enterpriseInformation.getIndustryid());
       r.setType(enterpriseInformation.getScaleid());
       
       debtRisk debtRisk = debtRiskMapper.selectDebtRisk(r);
       qualityAssets qualityAssets = qualityAssetsMapper.selectQualityAssets(r);
       profitability profitability = profitabilityMapper.selectProfitability(r);
       economicGain economicGain = economicGainMapper.selectEconomicGain(r);
       List<balanceSheet> balancesheetList = balanceSheetMapper.selectBalanceSheetKey(id);
       balanceSheet balancesheet = balancesheetList.get(2);
       List<flows> flowsList = flowsMapper.selectFlowsKey(id);
       flows flows = flowsList.get(2);
       List<profit> profitList = profitMapper.selectProfitKey(id);
       profit profit = profitList.get(2);
       BigDecimal c = new BigDecimal(100);
       BigDecimal b = new BigDecimal(2);
       //资产负债率
       if(balancesheet.getTotalassets().doubleValue() == 0) {
    	   grande.setAssetsAndLiabilitie(0);
       }else {
    	   count=balancesheet.getTotalliability().multiply(c)
                   .divide(balancesheet.getTotalassets(), 2, RoundingMode.HALF_UP).doubleValue();
    	   
    	   if(count <= debtRisk.getAloadrate().doubleValue()) {
        	   grande.setAssetsAndLiabilitie(3);
           }else if(count >= debtRisk.getEloadrate().doubleValue()) {
        	   grande.setAssetsAndLiabilitie(0);
           }else {
        	   grande.setAssetsAndLiabilitie(debtRisk.getAloadrate().doubleValue()/count*3);
           }
       }
       
    
       
       
       //现金流动负债比率
       if(balancesheet.getTotalcurrentliability().doubleValue() == 0) {
    	   grande.setCashFlow(0);
       }else {
    	   count=flows.getNetcashflowfromactivities().multiply(c)
                   .divide(balancesheet.getTotalcurrentliability(), 2, RoundingMode.HALF_UP).doubleValue();
    	   
    	   if(count >= debtRisk.getAflow().doubleValue()) {
        	   grande.setCashFlow(3);
           }else if(count <= debtRisk.getEflow().doubleValue()) {
        	   grande.setCashFlow(0);
           }else {
        	   BigDecimal bg = new BigDecimal(count/debtRisk.getAflow().doubleValue()*3);
    	       double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        	   grande.setCashFlow(f1);
           }
       }
      
       
       
       
       //速动比率
       if(balancesheet.getTotalcurrentliability().doubleValue() == 0) {
    	   grande.setQuickRatio(0);
       }else {
    	   count=balancesheet.getTotalcurrentassets().subtract(balancesheet.getInventory()).multiply(c)
   				.divide(balancesheet.getTotalcurrentliability(), 2, RoundingMode.HALF_UP).doubleValue();
    	   
    	   if(count >= debtRisk.getAquickaction().doubleValue()) {
        	   grande.setQuickRatio(3);
           }else if(count <= debtRisk.getEquickaction().doubleValue()) {
        	   grande.setQuickRatio(0);
           }else {
        	   BigDecimal bg = new BigDecimal(count/debtRisk.getAquickaction().doubleValue()*3);
    	        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        	   grande.setQuickRatio(f1);
           }
       }
       
       
       
       
       
       //总资产周转率
       balanceSheet balancesheet1 = balancesheetList.get(1);
       
       if(balancesheet.getTotalassets().add(balancesheet1.getTotalassets()).doubleValue() == 0) {
    	   grande.setTurnoverOfTotalCapital(0);
       }else {
    	   count=profit.getOperatingreceipt()
                   .divide(balancesheet.getTotalassets().add(balancesheet1.getTotalassets()).divide(b), 2, RoundingMode.HALF_UP).doubleValue();
    	   
    	   if(count >= qualityAssets.getAtotalassets().doubleValue()) {
        	   grande.setTurnoverOfTotalCapital(3);
           }else if(count <= qualityAssets.getEtotalassets().doubleValue()) {
        	   grande.setTurnoverOfTotalCapital(0);
           }else {
        	   BigDecimal bg = new BigDecimal(count/qualityAssets.getAtotalassets().doubleValue()*3);
    	        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        	   grande.setTurnoverOfTotalCapital(f1);
           }
       }
      
       
       
       
       
       //应收账款周转率
       if(balancesheet.getReceivables().add(balancesheet1.getReceivables()).divide(b).doubleValue() == 0) {
    	   grande.setReceivables(0);
       }else {
    	   count=profit.getOperatingreceipt()
   				.divide(balancesheet.getReceivables().add(balancesheet1.getReceivables()).divide(b), 2, RoundingMode.HALF_UP).doubleValue();
    	   
    	   if(count >= qualityAssets.getAreceivables().doubleValue()) {
		    	 grande.setReceivables(3);
		     }else if(count <= qualityAssets.getEreceivables().doubleValue()) {
		    	 grande.setReceivables(0);
		     }else {
		    	 BigDecimal bg = new BigDecimal(count/qualityAssets.getAreceivables().doubleValue()*3);
			        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		    	 grande.setReceivables(f1);
		     }
       }
		
		
		
		//流动资产周转率
		if(balancesheet.getTotalcurrentassets().add(balancesheet1.getTotalcurrentassets()).doubleValue() == 0) {
			grande.setCurrentAssets(0);
		}else {
			count=profit.getOperatingreceipt()
					.divide(balancesheet.getTotalcurrentassets().add(balancesheet1.getTotalcurrentassets()).divide(b), 2, RoundingMode.HALF_UP).doubleValue();
			
			if(count >= qualityAssets.getAflow().doubleValue()) {
		    	 grande.setCurrentAssets(3);
		     }else if(count <= qualityAssets.getEflow().doubleValue()) {
		    	 grande.setCurrentAssets(0);
		     }else {
		    	 BigDecimal bg = new BigDecimal(count/qualityAssets.getAflow().doubleValue()*3);
			        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		    	 grande.setCurrentAssets(f1);
		     }
		}
		
		
		
		
		//净资产收益率
		if(balancesheet.getTotalownersequity().add(balancesheet1.getTotalownersequity()).doubleValue() == 0) {
			grande.setNetAsset(0);
		}else {
			count=profit.getRetainedprofits().multiply(c)
					.divide(balancesheet.getTotalownersequity().add(balancesheet1.getTotalownersequity()).divide(b), 2, RoundingMode.HALF_UP).doubleValue();
			if(count >= profitability.getAnetasset().doubleValue()) {
		    	 grande.setNetAsset(3);
		     }else if(count <= profitability.getEnetasset().doubleValue()) {
		    	 grande.setNetAsset(0);
		     }else {
		    	 BigDecimal bg = new BigDecimal(count/profitability.getAnetasset().doubleValue()*3);
			        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		    	 grande.setNetAsset(f1);
		     }
		}
		
		
		
		
		//营业收入利润率
		if(profit.getOperatingreceipt().doubleValue() == 0) {
			grande.setTaking(0);
		}else {
			count=profit.getOperatingreceipt().subtract(profit.getOperatingcosts()).subtract(profit.getBusinesstax()).subtract(profit.getSellingexpenses())
				      .subtract(profit.getHandlingexpense()).subtract(profit.getFinancialexpense()).subtract(profit.getAssetsimpairmentloss())
				      .add(profit.getFairvalue()).add(profit.getIncomefrominvestment()).add(profit.getNonbusinessincome()).multiply(c)
				      .divide(profit.getOperatingreceipt(), 2, RoundingMode.HALF_UP).doubleValue();
			
			
				if(count >= profitability.getAmarket().doubleValue()) {
			    	 grande.setTaking(3);
			     }else if(count < profitability.getEmarket().doubleValue()) {
			    	 grande.setTaking(0);
			     }else {
			    	 BigDecimal bg = new BigDecimal(count/profitability.getAmarket().doubleValue()*3);
				     double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			    	 grande.setTaking(f1);
			     }
		}
		
		

		//总资产报酬率
		
		if(balancesheet.getTotalassets().add(balancesheet1.getTotalassets()).doubleValue() == 0) {
			grande.setTotalAssets(0);
		}else {
			count=profit.getTotalprofit().add(profit.getInterestexpense()).multiply(c)
					.divide(balancesheet.getTotalassets().add(balancesheet1.getTotalassets()).divide(b), 2, RoundingMode.HALF_UP).doubleValue();
			
			if(count >= profitability.getAtotalassets().doubleValue()) {
		    	 grande.setTotalAssets(3);
		     }else if(count <= profitability.getEtotalassets().doubleValue()) {
		    	 grande.setTotalAssets(0);
		     }else {
		    	 BigDecimal bg = new BigDecimal(count/profitability.getAtotalassets().doubleValue()*3);
			        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		    	 grande.setTotalAssets(f1);
		     }
		}
		
		
		
		
		
		//研发成果
	       patentType patentType = patentTypeMapper.selectPatentType(id);
	      if(patentType == null) {
	    	  grande.setResearchAchievement(0);
	      }else {
	    	  count = (patentType.getInvent()*0.5)+(patentType.getPractical()*0.2)+(patentType.getDesign()*0.1);
		       
		       if(count >= 3) {
		    	   grande.setResearchAchievement(3);
		       }else {
		    	   grande.setResearchAchievement(count);
		       }
	      }
	       
	       
	       
		//营业收入增长率
		profit profit1 = profitList.get(1); 
		count=profit1.getOperatingreceipt().multiply(c)
				.divide(profit.getOperatingprofit().subtract(profit1.getOperatingprofit()), 2, RoundingMode.HALF_UP).doubleValue();
		
		if(count >= economicGain.getAmarket().doubleValue()) {
	    	 grande.setIncreaseRateOfBusinessRevenue(2);
	     }else if(count > economicGain.getBmarket().doubleValue()) {
	    	 grande.setIncreaseRateOfBusinessRevenue(1.6);
	     }else if(count > economicGain.getCmarket().doubleValue()) {
	    	 grande.setIncreaseRateOfBusinessRevenue(1.3);
	     }else if(count > economicGain.getDmarket().doubleValue()) {
	    	 grande.setIncreaseRateOfBusinessRevenue(1);
	     }else if(count > economicGain.getEmarket().doubleValue()) {
	    	 grande.setIncreaseRateOfBusinessRevenue(0.5);
	     }else {
	    	 grande.setIncreaseRateOfBusinessRevenue(0);
	     }
		
		
		//资本保值增长率
		count=profit1.getTotalprofit().multiply(c)
				.divide(profit.getTotalprofit(), 2, RoundingMode.HALF_UP).doubleValue();
		if(count >= economicGain.getAhedging().doubleValue()) {
	    	 grande.setGrowthRateOfCapitalPreservation(2);
	     }else if(count > economicGain.getBhedging().doubleValue()) {
	    	 grande.setGrowthRateOfCapitalPreservation(1.6);
	     }else if(count > economicGain.getChedging().doubleValue()) {
	    	 grande.setGrowthRateOfCapitalPreservation(1.3);
	     }else if(count > economicGain.getDhedging().doubleValue()) {
	    	 grande.setGrowthRateOfCapitalPreservation(1);
	     }else if(count > economicGain.getEhedging().doubleValue()) {
	    	 grande.setGrowthRateOfCapitalPreservation(0.5);
	     }else {
	    	 grande.setGrowthRateOfCapitalPreservation(0);
	     }
		
		
		//营业利润增长率
		if(profit1.getOperatingreceipt().subtract(profit1.getOperatingcosts()).subtract(profit1.getBusinesstax()).doubleValue() == 0) {
			grande.setGrowthRateOfOperatingProfit(0);
		}else {
			count=(profit.getOperatingreceipt().subtract(profit.getOperatingcosts()).subtract(profit.getBusinesstax())).
					   subtract(profit1.getOperatingreceipt().subtract(profit1.getOperatingcosts()).subtract(profit1.getBusinesstax()))
					   .divide(profit1.getOperatingreceipt().subtract(profit1.getOperatingcosts()).subtract(profit1.getBusinesstax()), 2, RoundingMode.HALF_UP).doubleValue();
			if(count >= economicGain.getAprofit().doubleValue()) {
		    	 grande.setGrowthRateOfOperatingProfit(2);
		     }else if(count > economicGain.getBprofit().doubleValue()) {
		    	 grande.setGrowthRateOfOperatingProfit(1.6);
		     }else if(count > economicGain.getCprofit().doubleValue()) {
		    	 grande.setGrowthRateOfOperatingProfit(1.3);
		     }else if(count > economicGain.getDprofit().doubleValue()) {
		    	 grande.setGrowthRateOfOperatingProfit(1);
		     }else if(count > economicGain.getEprofit().doubleValue()) {
		    	 grande.setGrowthRateOfOperatingProfit(0.5);
		     }else {
		    	 grande.setGrowthRateOfOperatingProfit(0);
		     }
		}
		
		
		
		
		
		//社会贡献率
		socialContribution socialContribution = socialContributionMapper.selectSocialContribution(id);
		count=socialContribution.getContribution().doubleValue()/socialContribution.getAverageassets().doubleValue()*100;
		
		if(count >= 20 ) {
			grande.setSocialContributionRate(4);
		}else {
			BigDecimal bg = new BigDecimal(count/20*4);
	        double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
			grande.setSocialContributionRate(f1);
		}
       
		
		
		
		//行政监管信用记录
		List<administrative> administrativeList = administrativeMapper.selectAdministrative(id);
		if(administrativeList.isEmpty()) {
			grande.setAdministrative(10);
		}else {
			count=10-(administrativeList.size()*2);
			if(count < 0) {
				grande.setAdministrative(0);
			}else {
				grande.setAdministrative(count);
			}
		}
		
		
		
		//涉及劳动争议、人事争议案件
		List<violationOperation> violationOperationList = violationOperationMapper.selectViolationOperation(id);
		if(violationOperationList.isEmpty()) {
			grande.setLaborDispute(5);
		}else {
			count = 5-violationOperationList.size();
			if(count < 0) {
				grande.setLaborDispute(0);
			}else {
				grande.setLaborDispute(count);
			}
		}
		
		
		//司法监管信用记录（涉及合同纠纷案件、人格权纠纷、知识产权与竞争纠纷、物权纠纷、海事海商纠纷、与公司证券保险票据等有关的纠纷、侵权责任纠纷、行贿罪、受贿罪案件）
		List<judicial> judicialList = judicialMapper.selectJudicial(id);
		if(judicialList.isEmpty()) {
			grande.setJudicialSupervision(6);
		}else {
			count = 6 - judicialList.size();
			if(count < 0) {
				grande.setJudicialSupervision(0);
			}else {
				grande.setJudicialSupervision(count);
			}
		}
		
		
		//按时偿还贷款本金及利息
		creditLoan creditLoan = creditLoanMapper.selectCreditLoan(id);
		if(creditLoan.getIndebt() == 0) {
			grande.setRepay(5);
		}else {
			grande.setRepay(0);
		}
		if(enterpriseInformation.getScience() == 0) {
			grande.setResearch(0);
		}else {
			grande.setResearch(3);
		}
		
       
		return ServerResponse.createBySuccess(grande);
	}
	
	
}
